package com.zmzncs.lmtc.module.park.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.pojo.AuthorizeVO;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.park.pojo.park_manager.*;
import com.zmzncs.lmtc.module.park.service.ParkManagerService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 * 车场管理员表 前端控制器
 * </p>
 * @author 高博文
 * @since 2020-12-09
 */
@Api(tags = {"车场管理员表"})
@RestController
@RequestMapping("/park/park-manager")
public class ParkManagerController {

    @Autowired
    private ParkManagerService parkManagerService;

    @ApiOperation(value = "微信授权")
    @PostMapping(value = "/authorize")
    public Rest<ParkManagerDTO> authorize(@Validated WeiXinAuthorizeForm weiXinAuthorizeForm) {
        ParkManagerDTO parkManagerDTO = parkManagerService.authorize(weiXinAuthorizeForm);
        return RestUtil.success(parkManagerDTO);
    }

    @ApiOperation(value = "新增车场管理员表")
    @PostMapping("add")
    public Rest<Map<String,String>> add(@RequestBody @Valid ParkManagerAddForm form){
        Map<String, String> add = parkManagerService.add(form);
        if ("0".equals(add.get("status"))){
            return RestUtil.error(add.get("message"));
        }
        return RestUtil.success(add);
    }

    @ApiOperation(value = "删除车场管理员表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "车场管理ID", required = true),
    })
    @PostMapping("delete/{id}")
    public Rest delete(@PathVariable Long id){
        parkManagerService.delete(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "更新车场管理员表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping("update")
    public Rest update(@RequestBody @Valid ParkManagerUpdateForm form){
        parkManagerService.update(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "查询车场管理员表分页数据")
    @GetMapping("page")
    public Rest<IPage<ParkManagerDTO>> page(@Valid ParkManagerSearchForm form){
        IPage<ParkManagerDTO> iPage = parkManagerService.page(form);

        return RestUtil.success(iPage);
    }

    @ApiOperation(value = "根据id查询车场管理员表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车场管理ID", required = true),
    })
    @GetMapping("get-by-id")
    public Rest<ParkManagerVO> getById(@RequestParam Long id){
        ParkManagerDTO parkManagerDTO = parkManagerService.getById(id);

        return RestUtil.success(parkManagerDTO);
    }

    @ApiOperation(value = "获取短信验证码")
    @GetMapping("get-verify-code")
    public Rest getVerifyCode(@RequestParam String phoneNumber){
        Map<String, Object> verifyCode = parkManagerService.sendVerifyCode(phoneNumber);

        return RestUtil.success(verifyCode);
    }

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public Rest<AuthorizeVO> login(@RequestBody @Valid LoginForm form){
        AuthorizeVO login = parkManagerService.login(form);

        return RestUtil.success(login);
    }

}
